import service from "../../request.js";
import {ElMessage, ElMessageBox} from "element-plus";

//下载文件
export async  function downLoadFile(path){
    try {
        let blob = await service({
            method: 'GET',
            url: '/common/download/' + path,
            responseType: 'blob',

        })
        //模拟一个点击下载
        //获取文件名
        let filename = path.split("_").slice(1,).join("_")

        // 创建临时下载链接并触发下载
        const url = window.URL.createObjectURL(blob)
        const link = document.createElement('a')
        link.href = url
        link.setAttribute('download', filename) // 设置下载的文件名
        document.body.appendChild(link)
        link.click()
        link.remove()
        window.URL.revokeObjectURL(url)
    } catch (e) {
        console.log("e",e)
        ElMessage({message: '下载失败', type: 'warning',})
    }
}


//封装确认框
export async function confirm(content){
    try {
         await ElMessageBox.confirm(
            content,
            '重要提示',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
        return true
    } catch (e) {
        return false
    }
}

//<a href="下载地址" download="文件名">下载</a>">
